package com.uit.gis.pojo;

import processing.data.JSONArray;
import processing.data.JSONObject;

import com.uit.gis.util.JsonData;
import com.uit.gis.util.Util.SpatialType;

public class Arc extends Shape {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void setup() {
		spatialType = SpatialType.ARCS;
	}

	JSONArray arcs = new JSONArray();
	JSONObject vertex = new JSONObject();
	JSONObject cVertex;
	int i;

	@Override
	public void draw() {
		background(255);
		smooth();
		noFill();
		stroke(0);

		if (arcs.size() > 0) {
			beginShape();

			for (i = 0; i < arcs.size(); i++) {
				cVertex = arcs.getJSONObject(i);
				curveVertex(cVertex.getInt("x"), cVertex.getInt("y"));

				if (i == 0 || i == arcs.size() - 1) {
					curveVertex(cVertex.getInt("x"), cVertex.getInt("y"));
				}
			}

			endShape();

			fill(255, 0, 0);
			noStroke();
			for (i = 0; i < arcs.size(); i++) {
				cVertex = arcs.getJSONObject(i);
				ellipse(cVertex.getInt("x"), cVertex.getInt("y"), 3, 3);
			}
		}
	}

	@Override
	public void mousePressed() {
		vertex = new JSONObject();
		vertex.setInt("x", mouseX);
		vertex.setInt("y", mouseY);
		arcs.append(vertex);
	}

	@Override
	public void mouseClicked(processing.event.MouseEvent e) {
		if (e.getButton() == processing.core.PConstants.RIGHT) {
			// saveJSONArray(arrayJson, "lines.json");
			saveToTextFile();
			continueDraw = false;
		}
	}

	@Override
	public void saveToTextFile() {
		// TODO Auto-generated method stub
		saveJSONArray(arcs, getSavingPath());
		super.saveToTextFile();
	};

	@Override
	public JsonData loadDataFromFile() {
		// TODO Auto-generated method stub
		JSONArray array = loadJSONArray(getSavingPath());
		JsonData jsonData = new JsonData(array, null);
		return jsonData;
	}
}
